package com.ruoyi.web.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.HbsTableName;
import com.ruoyi.hbs.domain.*;
import com.ruoyi.hbs.service.*;
import com.ruoyi.web.controller.hbs.HbsBusinessController;
import com.ruoyi.web.convert.HbsDataConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * HBS数据同步服务实现
 */
@Service
@Slf4j
public class HbsDataSyncServiceImpl {

    @Autowired
    private HbsBusinessController hbsBusinessController;

    @Autowired
    private ProductArchiveService productArchiveService;

    @Autowired
    private HbsBusinessArchiveService hbsBusinessArchiveService;
    @Autowired
    private HbsTerminalArchiveService hbsTerminalArchiveService;

    @Autowired
    private MsfxBusinessStatisticsService msfxBusinessStatisticsService;
    @Autowired
    private MsfxTerminalStatisticsService msfxTerminalStatisticsService;
    @Autowired
    private MsfxTerminalSalesFirstOrderService msfxTerminalSalesFirstOrderService;

    @Autowired
    private HbsDataConverter hbsDataConverter;


    /**
     * 同步数据(采集数据同步到伙伴云)
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncDataToHby(JSONObject bodyJson) throws Exception {
        // 1. 获取表格列表、表格配置和数据列表
        tableResult result = getTableResult(bodyJson);
        String tableConfigName = bodyJson.get("tableConfigName").toString();
        if (tableConfigName.equals(HbsTableName.BUSSINESS.getTableName())) {
            syncBusinessStatisticsData(result);
        } else if (tableConfigName.equals(HbsTableName.TERMINAL.getTableName())) {
            syncTerminalStatisticsData(result);
        } else if (tableConfigName.equals(HbsTableName.TERMINAL_FIRST.getTableName())) {
            syncTerminalSalesFirstOrderData(result);
        }

    }

    /**
     * 同步商业统计数据到伙伴云
     *
     * @param tableRe
     * @throws Exception 异常
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncBusinessStatisticsData(tableResult tableRe) throws Exception {
        // 2. 获取表列表并提取 table_id
        Map<String, String> fieldNameToFieldId = getFieldNameToFieldId(tableRe);
        // 4. 查询本地数据（msfx_business_statistics）
        List<MsfxBusinessStatistics> statisticsList = msfxBusinessStatisticsService.syncBusinessStatisticsDataList();
        // 5. 构建要上传的数据 items
        List<Map<String, Object>> items = new ArrayList<>();
        for (MsfxBusinessStatistics stat : statisticsList) {
            Map<String, Object> itemMap = new HashMap<>();

            // 添加item_id 字段（用于唯一标识记录）
            String itemId = fieldNameToFieldId.get("主键id");
            // 构建fields对象
            Map<String, Object> fields = new HashMap<>();
            // 字段映射示例（根据实际 HBS 字段名调整）
            String[] productIdArray = {stat.getHbsProductId() != null ? stat.getHbsProductId() : ""};
            String[] businessIdArray = {stat.getHbsBusinessId() != null ? stat.getHbsBusinessId() : ""};
            fields.put(itemId, stat.getId());
            fields.put(fieldNameToFieldId.get("产品名称"), productIdArray);
            fields.put(fieldNameToFieldId.get("商业名称"), businessIdArray);
            fields.put(fieldNameToFieldId.get("期初库存"), stat.getInitialStock());
            fields.put(fieldNameToFieldId.get("出库数量"), stat.getOutboundQuantity());
            fields.put(fieldNameToFieldId.get("入库数量"), stat.getInboundQuantity());
            fields.put(fieldNameToFieldId.get("预计库存"), stat.getExpectedStock());
            fields.put(fieldNameToFieldId.get("统计日期"), formatDate(stat.getStatDate()));
            // 将fields对象添加到itemMap中
            itemMap.put("fields", fields);
            items.add(itemMap);
        }


        // 6. 分批上传数据（每批最多 100 条）
        int batchSize = 100;
        String tableId = tableRe.tableId;
        int totalUploaded = 0; // 新增：用于累计已上传的数据总量

        for (int i = 0; i < items.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, items.size());
            List<Map<String, Object>> batch = items.subList(i, endIndex);

            JSONObject request = new JSONObject();
            request.set("table_id", tableId);
            request.set("update_type", "upsert"); // 使用 upsert 模式
            request.set("update_by_fields", Arrays.asList(fieldNameToFieldId.get("主键id"))); // 指定更新依据字段为 item_id
            request.set("items", batch);
            log.info(request.toString());
            R result = hbsBusinessController.openapiV1Items(request);
            if (result != null && R.isSuccess(result)) {
                totalUploaded += batch.size(); // 累加本次上传的数据量

            } else {
                log.error("商业统计数据上传失败: {}", result != null ? result.getMsg() : "未知错误");
                throw new Exception("商业统计数据上传失败," + (result != null ? result.getMsg() : "未知错误"));
            }
        }
        log.info("成功上传 {} 条商业统计数据，总共已上传 {} 条",items.size(), totalUploaded);
        // 更新已上传记录的状态
        if (totalUploaded > 0) {
            for (MsfxBusinessStatistics stat : statisticsList) {
                stat.setIsUploaded(1); // 设置为已上传

                // 根据生成的 item_id 更新数据库记录
                LambdaQueryWrapper<MsfxBusinessStatistics> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MsfxBusinessStatistics::getProductId, stat.getProductId())
                        .eq(MsfxBusinessStatistics::getBusinessId, stat.getBusinessId());

                msfxBusinessStatisticsService.update(stat, queryWrapper);
            }
            log.info("商业统计数据同步完成，共上传 {} 条数据", totalUploaded);
        }
    }

    /**
     * 同步终端统计数据到伙伴云
     *
     * @param tableRe
     * @throws Exception 异常
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncTerminalStatisticsData(tableResult tableRe) throws Exception {
        // 2. 获取表列表并提取 table_id
        Map<String, String> fieldNameToFieldId = getFieldNameToFieldId(tableRe);
        // 4. 查询本地数据（msfx_terminal_statistics）
        List<MsfxTerminalStatistics> statisticsList = msfxTerminalStatisticsService.syncTerminalStatisticsDataList();
        // 5. 构建要上传的数据 items
        List<Map<String, Object>> items = new ArrayList<>();
        for (MsfxTerminalStatistics stat : statisticsList) {
            Map<String, Object> item = new HashMap<>();
            // 添加item_id 字段（用于唯一标识记录）
            String itemId = fieldNameToFieldId.get("主键id");
            // 构建fields对象
            Map<String, Object> fields = new HashMap<>();
            // 字段映射示例（根据实际 HBS 字段名调整）
            String[] productIdArray = {stat.getHbsProductId() != null ? stat.getHbsProductId() : ""};
            String[] businessIdArray = {stat.getHbsBusinessId() != null ? stat.getHbsBusinessId() : ""};
            fields.put(itemId, stat.getId());
            fields.put(fieldNameToFieldId.get("产品名称"), productIdArray);
            fields.put(fieldNameToFieldId.get("商业名称"), businessIdArray);
            fields.put(fieldNameToFieldId.get("入库数量"), stat.getInboundQuantity());
            fields.put(fieldNameToFieldId.get("统计日期"), formatDate(stat.getStatDate()));
            // 将fields对象添加到itemMap中
            item.put("fields", fields);
            items.add(item);
        }
        // 6. 分批上传数据（每批最多 100 条）
        int batchSize = 100;
        String tableId = tableRe.tableId;
        int totalUploaded = 0; // 新增：用于累计已上传的数据总量
        for (int i = 0; i < items.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, items.size());
            List<Map<String, Object>> batch = items.subList(i, endIndex);
            JSONObject request = new JSONObject();
            request.set("table_id", tableId);
            request.set("update_type", "upsert"); // 使用 upsert 模式
            request.set("update_by_fields", Arrays.asList(fieldNameToFieldId.get("主键id"))); // 指定更新依据字段为 item_id
            request.set("items", batch);
            log.info(request.toString());
            R result = hbsBusinessController.openapiV1Items(request);
            if (result != null && R.isSuccess(result)) {
                totalUploaded += batch.size(); // 累加本次上传的数据量

            } else {
                log.error("终端统计数据上传失败: {}", result != null ? result.getMsg() : "未知错误");
                throw new Exception("终端统计数据上传失败," + (result != null ? result.getMsg() : "未知错误"));
            }
        }
        log.info("成功上传 {} 条终端统计数据，总共已上传 {} 条", items.size(), totalUploaded);
        // 更新已上传记录的状态
        if (totalUploaded > 0) {
            for (MsfxTerminalStatistics stat : statisticsList) {
                stat.setIsUploaded(1); // 设置为已上传

                // 根据生成的 item_id 更新数据库记录
                LambdaQueryWrapper<MsfxTerminalStatistics> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MsfxTerminalStatistics::getProductId, stat.getProductId())
                        .eq(MsfxTerminalStatistics::getBusinessId, stat.getBusinessId());

                msfxTerminalStatisticsService.update(stat, queryWrapper);
            }
            log.info("终端统计数据同步完成，共上传 {} 条数据", totalUploaded);
        }
    }

    /**
     * 同步终端销售首单数据到伙伴云
     *
     * @param tableRe
     * @throws Exception 异常
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncTerminalSalesFirstOrderData(tableResult tableRe) throws Exception {
        // 2. 获取表列表并提取 table_id
        Map<String, String> fieldNameToFieldId = getFieldNameToFieldId(tableRe);
        // 4. 查询本地数据（msfx_terminal_sales_first_order）
        List<MsfxTerminalSalesFirstOrder> firstOrderList = msfxTerminalSalesFirstOrderService.syncTerminalSalesFirstOrderDataList();
        // 5. 构建要上传的数据 items
        List<Map<String, Object>> items = new ArrayList<>();
        for (MsfxTerminalSalesFirstOrder order : firstOrderList) {
            Map<String, Object> item = new HashMap<>();
            // 添加item_id 字段（用于唯一标识记录）
            String itemId = fieldNameToFieldId.get("主键id");
            // 构建fields对象
            Map<String, Object> fields = new HashMap<>();
            // 字段映射示例（根据实际 HBS 字段名调整）
            String[] productIdArray = {order.getHbsProductId() != null ? order.getHbsProductId().toString() : ""};
            String[] terminalIdArray = {order.getHbsTerminalId() != null ? order.getHbsTerminalId().toString() : ""};
            fields.put(itemId, order.getId());
            fields.put(fieldNameToFieldId.get("终端名称"), terminalIdArray);
            fields.put(fieldNameToFieldId.get("产品名称"), productIdArray);
            fields.put(fieldNameToFieldId.get("首次销售日期/单据时间"), formatDate(order.getFirstSaleDate()));
            fields.put(fieldNameToFieldId.get("上传时间"), formatDate(order.getCreateTime()));
            // 将fields对象添加到itemMap中
            item.put("fields", fields);
            items.add(item);
        }
        // 6. 分批上传数据（每批最多 100 条）
        int batchSize = 100;
        int totalUploaded = 0; // 新增：用于累计已上传的数据总量
        String tableId = tableRe.tableId;
        for (int i = 0; i < items.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, items.size());
            List<Map<String, Object>> batch = items.subList(i, endIndex);

            JSONObject request = new JSONObject();
            request.set("table_id", tableId);
            request.set("update_type", "upsert"); // 使用 upsert 模式
            request.set("update_by_fields", Arrays.asList(fieldNameToFieldId.get("主键id"))); // 指定更新依据字段为 item_id
            request.set("items", batch);
            R result = hbsBusinessController.openapiV1Items(request);
            if (result != null && R.isSuccess(result)) {
                totalUploaded += batch.size(); // 累加本次上传的数据量

            } else {
                log.error("终端销售首单数据上传失败: {}", result != null ? result.getMsg() : "未知错误");
                throw new Exception("终端销售首单数据上传失败," + (result != null ? result.getMsg() : "未知错误"));
            }
        }
        log.info("成功上传 {} 条终端销售首单数据，总共已上传 {} 条", items.size(), totalUploaded);
        // 更新已上传记录的状态
        if (totalUploaded > 0) {
            for (MsfxTerminalSalesFirstOrder order : firstOrderList) {
                order.setIsUploaded(1); // 设置为已上传
                // 根据生成的 item_id 更新数据库记录
                LambdaQueryWrapper<MsfxTerminalSalesFirstOrder> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MsfxTerminalSalesFirstOrder::getProductMappingId, order.getProductMappingId())
                        .eq(MsfxTerminalSalesFirstOrder::getTerminalMappingId, order.getTerminalMappingId());
                msfxTerminalSalesFirstOrderService.update(order, queryWrapper);
            }
            log.info("终端销售首单数据同步完成，共上传 {} 条数据", totalUploaded);
        }
    }

    /**
     * 格式化日期
     */
    private String formatDate(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取表结构，构建 字段名 -> field_id 映射
     */
    private static Map<String, String> getFieldNameToFieldId(tableResult tableResult) {
        // 2. 获取表列表并提取 table_id
        R tableConfigResponse = tableResult.tableConfigResponse;
        // 3. 获取表结构，构建 字段名 -> field_id 映射
        JSONObject tableConfig = (JSONObject) tableConfigResponse.getData();
        JSONObject tableInfo = tableConfig.getJSONObject("table");
        JSONArray fields = tableInfo.getJSONArray("fields");

        Map<String, String> fieldNameToFieldId = new HashMap<>();
        for (int i = 0; i < fields.size(); i++) {
            JSONObject field = fields.getJSONObject(i);
            String fieldName = field.getStr("name"); // 中文字段名
            String fieldId = field.getStr("field_id");
            fieldNameToFieldId.put(fieldName, fieldId);
        }
        return fieldNameToFieldId;
    }

    /**
     * 同步产品档案数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncProductArchiveData(JSONObject bodyJson) throws Exception {
        // 1. 获取表格列表、表格配置和数据列表
        tableResult result = getTableResult(bodyJson);
        // 2. 提取过滤条件
        JSONObject filter = bodyJson.getJSONObject("filter");
        // 3. 处理并插入数据（使用分页获取）
        List<ProductArchive> productArchives = processProductArchiveDataParallel(result.tableConfigResponse, result.tableId, filter);
        batchInsertProductArchivesOptimized(productArchives);
    }

    /**
     * 并行处理数据并转换为 ProductArchive 对象
     */
    private List<ProductArchive> processProductArchiveDataParallel(R tableConfigResponse, String tableId, JSONObject filter) throws Exception {
        List<ProductArchive> result = new ArrayList<>();

        // 解析表格配置（同原方法）
        JSONObject tableConfig = (JSONObject) tableConfigResponse.getData();
        if (tableConfig == null) {
            return result;
        }

        JSONObject tableInfo = tableConfig.getJSONObject("table");
        if (tableInfo == null) {
            return result;
        }

        JSONArray fields = tableInfo.getJSONArray("fields");

        // 构建字段映射（同原方法）
        Map<String, JSONObject> fieldNameConfigMap = new HashMap<>();
        Map<String, String> fieldIdToNameMap = new HashMap<>();

        if (fields != null) {
            for (int i = 0; i < fields.size(); i++) {
                JSONObject field = fields.getJSONObject(i);
                String fieldName = field.getStr("name");
                fieldNameConfigMap.put(fieldName, field);
                String fieldId = field.getStr("field_id");
                fieldIdToNameMap.put(fieldId, fieldName);
            }
        }

        // 分页获取所有数据项
        List<Object> allItems = getItemsByPageParallel(tableId, 100, filter); // 增大批量大小

        // 并行处理数据转换
        int processors = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(processors);

        int chunkSize = Math.max(100, allItems.size() / processors);
        List<Future<List<ProductArchive>>> futures = new ArrayList<>();

        for (int i = 0; i < allItems.size(); i += chunkSize) {
            int endIndex = Math.min(i + chunkSize, allItems.size());
            List<Object> chunk = allItems.subList(i, endIndex);

            Future<List<ProductArchive>> future = executor.submit(() -> {
                List<ProductArchive> chunkResult = new ArrayList<>();
                for (Object itemObj : chunk) {
                    if (itemObj instanceof JSONObject) {
                        JSONObject item = (JSONObject) itemObj;
                        ProductArchive productArchive = convertItemToProductArchive(item, fieldNameConfigMap, fieldIdToNameMap);
                        if (productArchive != null) {
                            chunkResult.add(productArchive);
                        }
                    }
                }
                return chunkResult;
            });
            futures.add(future);
        }

        // 收集所有结果
        for (Future<List<ProductArchive>> future : futures) {
            try {
                result.addAll(future.get());
            } catch (Exception e) {
                log.error("收集转换结果失败", e);
            }
        }

        executor.shutdown();
        return result;
    }


    /**
     * 批量插入产品档案数据（优化版）
     */
    private void batchInsertProductArchivesOptimized(List<ProductArchive> productArchives) throws Exception {
        if (CollectionUtils.isEmpty(productArchives)) {
            return;
        }

        // 过滤掉ID为空的记录
        List<ProductArchive> validArchives = productArchives.stream()
                .filter(archive -> archive.getId() != null && !archive.getId().isEmpty())
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(validArchives)) {
            return;
        }

        // 增大批量处理批次大小
        int batchSize = 100;

        // 提取所有item_id
        List<String> itemIds = validArchives.stream()
                .map(ProductArchive::getId)
                .collect(Collectors.toList());

        // 并行查询已存在的记录
        Set<String> existingIds = Collections.emptySet();
        try {
            List<ProductArchive> existingArchives = productArchiveService.listByIds(itemIds);
            existingIds = existingArchives.stream()
                    .map(ProductArchive::getId)
                    .collect(Collectors.toSet());
        } catch (Exception e) {
            log.warn("查询现有记录失败，将全部作为新记录处理", e);
        }

        // 分离新增和更新的数据
        List<ProductArchive> toInsert = new ArrayList<>();
        List<ProductArchive> toUpdate = new ArrayList<>();

        for (ProductArchive archive : validArchives) {
            if (existingIds.contains(archive.getId())) {
//                toUpdate.add(archive);
            } else {
                toInsert.add(archive);
            }
        }

        // 并行批量插入新数据
        processBatchInParallel(toInsert, batchSize, productArchiveService::saveBatch);

        // 并行批量更新已有数据
//        processBatchInParallel(toUpdate, batchSize, productArchiveService::updateBatchById);
    }


    /**
     * 同步商业档案数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncBusinessArchiveData(JSONObject bodyJson) throws Exception {
        // 1. 获取表格列表、表格配置和数据列表
        tableResult result = getTableResult(bodyJson);
        // 2. 提取过滤条件
        JSONObject filter = bodyJson.getJSONObject("filter");
        // 3. 处理并插入数据（使用分页获取）
        List<HbsBusinessArchive> hbsBusinessArchives = processHbsBusinessArchiveDataParallel(result.tableConfigResponse, result.tableId, filter);
        batchInsertHbsBusinessArchivesOptimized(hbsBusinessArchives);
    }

    /**
     * 并行处理数据并转换为 HbsBusinessArchive 对象
     */
    private List<HbsBusinessArchive> processHbsBusinessArchiveDataParallel(R tableConfigResponse, String tableId, JSONObject filter) throws Exception {
        List<HbsBusinessArchive> result = new ArrayList<>();

        // 解析表格配置（同原方法）
        JSONObject tableConfig = (JSONObject) tableConfigResponse.getData();
        if (tableConfig == null) {
            return result;
        }

        JSONObject tableInfo = tableConfig.getJSONObject("table");
        if (tableInfo == null) {
            return result;
        }

        JSONArray fields = tableInfo.getJSONArray("fields");

        // 构建字段映射（同原方法）
        Map<String, JSONObject> fieldNameConfigMap = new HashMap<>();
        Map<String, String> fieldIdToNameMap = new HashMap<>();

        if (fields != null) {
            for (int i = 0; i < fields.size(); i++) {
                JSONObject field = fields.getJSONObject(i);
                String fieldName = field.getStr("name");
                fieldNameConfigMap.put(fieldName, field);
                String fieldId = field.getStr("field_id");
                fieldIdToNameMap.put(fieldId, fieldName);
            }
        }

        // 分页获取所有数据项
        List<Object> allItems = getItemsByPageParallel(tableId, 100, filter); // 增大批量大小

        // 并行处理数据转换
        int processors = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(processors);

        int chunkSize = Math.max(100, allItems.size() / processors);
        List<Future<List<HbsBusinessArchive>>> futures = new ArrayList<>();

        for (int i = 0; i < allItems.size(); i += chunkSize) {
            int endIndex = Math.min(i + chunkSize, allItems.size());
            List<Object> chunk = allItems.subList(i, endIndex);

            Future<List<HbsBusinessArchive>> future = executor.submit(() -> {
                List<HbsBusinessArchive> chunkResult = new ArrayList<>();
                for (Object itemObj : chunk) {
                    if (itemObj instanceof JSONObject) {
                        JSONObject item = (JSONObject) itemObj;
                        HbsBusinessArchive HbsBusinessArchive = convertItemToBusinessArchive(item, fieldNameConfigMap, fieldIdToNameMap);
                        if (HbsBusinessArchive != null) {
                            chunkResult.add(HbsBusinessArchive);
                        }
                    }
                }
                return chunkResult;
            });
            futures.add(future);
        }

        // 收集所有结果
        for (Future<List<HbsBusinessArchive>> future : futures) {
            try {
                result.addAll(future.get());
            } catch (Exception e) {
                log.error("收集转换结果失败", e);
            }
        }

        executor.shutdown();
        return result;
    }


    /**
     * 批量插入商业档案数据（优化版）
     */
    private void batchInsertHbsBusinessArchivesOptimized(List<HbsBusinessArchive> HbsBusinessArchives) throws Exception {
        if (CollectionUtils.isEmpty(HbsBusinessArchives)) {
            return;
        }

        // 过滤掉ID为空的记录
        List<HbsBusinessArchive> validArchives = HbsBusinessArchives.stream()
                .filter(archive -> archive.getId() != null && !archive.getId().isEmpty())
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(validArchives)) {
            return;
        }

        // 增大批量处理批次大小
        int batchSize = 100;

        // 提取所有item_id
        List<String> itemIds = validArchives.stream()
                .map(HbsBusinessArchive::getId)
                .collect(Collectors.toList());

        // 并行查询已存在的记录
        Set<String> existingIds = Collections.emptySet();
        try {
            List<HbsBusinessArchive> existingArchives = hbsBusinessArchiveService.listByIds(itemIds);
            existingIds = existingArchives.stream()
                    .map(HbsBusinessArchive::getId)
                    .collect(Collectors.toSet());
        } catch (Exception e) {
            log.warn("查询现有记录失败，将全部作为新记录处理", e);
        }

        // 分离新增和更新的数据
        List<HbsBusinessArchive> toInsert = new ArrayList<>();
        List<HbsBusinessArchive> toUpdate = new ArrayList<>();

        for (HbsBusinessArchive archive : validArchives) {
            if (existingIds.contains(archive.getId())) {
//                toUpdate.add(archive);
            } else {
                toInsert.add(archive);
            }
        }

        // 并行批量插入新数据
        processBatchInParallel(toInsert, batchSize, hbsBusinessArchiveService::saveBatch);

        // 并行批量更新已有数据
//        processBatchInParallel(toUpdate, batchSize, hbsBusinessArchiveService::updateBatchById);
    }


    /**
     * 同步终端档案数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncTerminalArchiveData(JSONObject bodyJson) throws Exception {
        // 1. 获取表格列表、表格配置和数据列表
        tableResult result = getTableResult(bodyJson);
        // 2. 提取过滤条件
        JSONObject filter = bodyJson.getJSONObject("filter");
        // 3. 处理并插入数据（使用分页获取）
        List<HbsTerminalArchive> hbsTerminalArchives = processHbsTerminalArchiveDataParallel(result.tableConfigResponse, result.tableId, filter);
        batchInsertHbsTerminalArchivesOptimized(hbsTerminalArchives);
//        List<HbsTerminalArchive> terminalArchives = processTerminalArchiveData(result.tableConfigResponse, result.tableId,filter);
//        batchInsertTerminalArchives(terminalArchives);
    }


    /**
     * 并行处理数据并转换为 HbsTerminalArchive 对象
     */
    private List<HbsTerminalArchive> processHbsTerminalArchiveDataParallel(R tableConfigResponse, String tableId, JSONObject filter) throws Exception {
        List<HbsTerminalArchive> result = new ArrayList<>();

        // 解析表格配置（同原方法）
        JSONObject tableConfig = (JSONObject) tableConfigResponse.getData();
        if (tableConfig == null) {
            return result;
        }

        JSONObject tableInfo = tableConfig.getJSONObject("table");
        if (tableInfo == null) {
            return result;
        }

        JSONArray fields = tableInfo.getJSONArray("fields");

        // 构建字段映射（同原方法）
        Map<String, JSONObject> fieldNameConfigMap = new HashMap<>();
        Map<String, String> fieldIdToNameMap = new HashMap<>();

        if (fields != null) {
            for (int i = 0; i < fields.size(); i++) {
                JSONObject field = fields.getJSONObject(i);
                String fieldName = field.getStr("name");
                fieldNameConfigMap.put(fieldName, field);
                String fieldId = field.getStr("field_id");
                fieldIdToNameMap.put(fieldId, fieldName);
            }
        }

        // 分页获取所有数据项
        List<Object> allItems = getItemsByPageParallel(tableId, 100, filter); // 增大批量大小

        // 并行处理数据转换
        int processors = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(processors);

        int chunkSize = Math.max(100, allItems.size() / processors);
        List<Future<List<HbsTerminalArchive>>> futures = new ArrayList<>();

        for (int i = 0; i < allItems.size(); i += chunkSize) {
            int endIndex = Math.min(i + chunkSize, allItems.size());
            List<Object> chunk = allItems.subList(i, endIndex);

            Future<List<HbsTerminalArchive>> future = executor.submit(() -> {
                List<HbsTerminalArchive> chunkResult = new ArrayList<>();
                for (Object itemObj : chunk) {
                    if (itemObj instanceof JSONObject) {
                        JSONObject item = (JSONObject) itemObj;
                        HbsTerminalArchive HbsTerminalArchive = convertItemToTerminalArchive(item, fieldNameConfigMap, fieldIdToNameMap);
                        if (HbsTerminalArchive != null) {
                            chunkResult.add(HbsTerminalArchive);
                        }
                    }
                }
                return chunkResult;
            });
            futures.add(future);
        }

        // 收集所有结果
        for (Future<List<HbsTerminalArchive>> future : futures) {
            try {
                result.addAll(future.get());
            } catch (Exception e) {
                log.error("收集转换结果失败", e);
            }
        }

        executor.shutdown();
        return result;
    }


    /**
     * 批量插入终端档案数据（优化版）
     */
    private void batchInsertHbsTerminalArchivesOptimized(List<HbsTerminalArchive> HbsTerminalArchives) throws Exception {
        if (CollectionUtils.isEmpty(HbsTerminalArchives)) {
            return;
        }

        // 过滤掉ID为空的记录
        List<HbsTerminalArchive> validArchives = HbsTerminalArchives.stream()
                .filter(archive -> archive.getId() != null && !archive.getId().isEmpty())
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(validArchives)) {
            return;
        }

        // 增大批量处理批次大小
        int batchSize = 100;

        // 提取所有item_id
        List<String> itemIds = validArchives.stream()
                .map(HbsTerminalArchive::getId)
                .collect(Collectors.toList());

        // 并行查询已存在的记录
        Set<String> existingIds = Collections.emptySet();
        try {
            List<HbsTerminalArchive> existingArchives = hbsTerminalArchiveService.listByIds(itemIds);
            existingIds = existingArchives.stream()
                    .map(HbsTerminalArchive::getId)
                    .collect(Collectors.toSet());
        } catch (Exception e) {
            log.warn("查询现有记录失败，将全部作为新记录处理", e);
        }

        // 分离新增和更新的数据
        List<HbsTerminalArchive> toInsert = new ArrayList<>();
        List<HbsTerminalArchive> toUpdate = new ArrayList<>();

        for (HbsTerminalArchive archive : validArchives) {
            if (existingIds.contains(archive.getId())) {
//                toUpdate.add(archive);
            } else {
                toInsert.add(archive);
            }
        }

        // 并行批量插入新数据
        processBatchInParallel(toInsert, batchSize, hbsTerminalArchiveService::saveBatch);

        // 并行批量更新已有数据
//        processBatchInParallel(toUpdate, batchSize, hbsTerminalArchiveService::updateBatchById);
    }

    /**
     * 并行处理批量操作
     */
    private <T> void processBatchInParallel(List<T> dataList, int batchSize,
                                            java.util.function.Consumer<List<T>> batchProcessor) {
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }

        ExecutorService executor = Executors.newFixedThreadPool(
                Math.min(4, (dataList.size() + batchSize - 1) / batchSize));

        List<Future<?>> futures = new ArrayList<>();

        for (int i = 0; i < dataList.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, dataList.size());
            List<T> batch = dataList.subList(i, endIndex);

            Future<?> future = executor.submit(() -> batchProcessor.accept(batch));
            futures.add(future);
        }

        // 等待所有批次完成
        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("批量处理失败", e);
            }
        }

        executor.shutdown();
    }

    /**
     * 将单个HBS数据项转换为ProductArchive对象
     */
    private ProductArchive convertItemToProductArchive(JSONObject item, Map<String, JSONObject> fieldNameConfigMap, Map<String, String> fieldIdToNameMap) {
        ProductArchive productArchive = new ProductArchive();

        // 设置主键
        String itemId = item.getStr("item_id");
        productArchive.setId(itemId);

        // 处理字段数据
        JSONObject fields = item.getJSONObject("fields");
        if (fields != null) {
            for (Map.Entry<String, Object> entry : fields.entrySet()) {
                String fieldId = entry.getKey();
                Object fieldValue = entry.getValue();

                // 通过字段ID找到字段名称
                String fieldName = fieldIdToNameMap.get(fieldId);
                if (fieldName != null) {
                    JSONObject fieldConfig = fieldNameConfigMap.get(fieldName);
                    if (fieldConfig != null) {
                        // 使用转换器处理字段值
                        String convertedValue = hbsDataConverter.convertFieldValue(fieldConfig, fieldValue);
                        // 根据字段名称映射到ProductArchive属性
                        setProductArchiveFieldByName(productArchive, fieldName, convertedValue);
                    }
                }
            }
        }

        // 设置创建和更新时间
        String createdOnStr = item.getStr("created_on");
        if (createdOnStr != null && !createdOnStr.isEmpty()) {
            productArchive.setCreatedOn(parseDateString(createdOnStr));
        }

        String updatedOnStr = item.getStr("updated_on");
        if (updatedOnStr != null && !updatedOnStr.isEmpty()) {
            productArchive.setUpdatedOn(parseDateString(updatedOnStr));
        }

        return productArchive;
    }

    /**
     * 根据字段名称设置ProductArchive对象的对应属性
     */
    private void setProductArchiveFieldByName(ProductArchive productArchive, String fieldName, String value) {
        switch (fieldName) {
            case "销售状态":
                productArchive.setSalesStatus(value);
                break;
            case "产品简称":
                productArchive.setProductShortName(value);
                break;
            case "产品全称":
                productArchive.setProductFullName(value);
                break;
            case "生产单位":
                productArchive.setProductionUnit(value);
                break;
            case "MAH单位":
                productArchive.setMahUnit(value);
                break;
            case "物料编码":
                productArchive.setMaterialCode(value);
                break;
            case "产品线":
                productArchive.setProductLine(value);
                break;
            case "KPI类别":
                productArchive.setKpiCategory(value);
                break;
            case "剂型":
                productArchive.setDosageForm(value);
                break;
            case "产品名称":
                productArchive.setProductName(value);
                break;
            case "规格":
                productArchive.setSpecification(value);
                break;
            case "包装规格":
                productArchive.setPackagingSpec(value);
                break;
            case "保质期":
                productArchive.setShelfLife(value);
                break;
            case "品牌":
                productArchive.setBrand(value);
                break;
            case "物料采购管理":
                productArchive.setMaterialProcurementMgmt(value);
                break;
            case "片(粒)/瓶(盒)":
                productArchive.setPiecesPerBottle(value);
                break;
            case "瓶(盒)/件(箱)":
                productArchive.setBottlesPerCase(value);
                break;
            case "万片(粒)/批/生产":
                productArchive.setPiecesPerBatch(value);
                break;
            case "理论产出(万片,万粒,万袋)":
                productArchive.setTheoreticalOutputPieces(value);
                break;
            case "理论产出(件,箱)":
                productArchive.setTheoreticalOutputCases(value);
                break;
            case "生产工序(道)":
                productArchive.setProductionProcessCount(value);
                break;
            case "总入库数(瓶,盒)":
                productArchive.setTotalInboundBottles(value);
                break;
            case "总出库数(瓶,盒)":
                productArchive.setTotalOutboundBottles(value);
                break;
            case "总出库数(件,箱)":
                productArchive.setTotalOutboundCases(value);
                break;
            case "现库存数(瓶,盒)":
                productArchive.setCurrentStockBottles(value);
                break;
            case "现库存数(件,箱)":
                productArchive.setCurrentStockCases(value);
                break;
            case "安全库存(件,箱)":
                productArchive.setSafetyStockCases(value);
                break;
            case "月均计划销量(件,箱)":
                productArchive.setMonthlyPlannedSalesCases(value);
                break;
            case "出库单位":
                productArchive.setOutboundUnit(value);
                break;
            case "年度指标":
                productArchive.setAnnualTarget(value);
                break;
            case "年度销售":
                productArchive.setAnnualSales(value);
                break;
            case "指标完成率":
                productArchive.setTargetCompletionRate(value);
                break;
            case "所属中心":
                productArchive.setCenter(value);
                break;
            case "一级部门":
                productArchive.setDepartmentLevel1(value);
                break;
            case "业务部门（三级）":
                productArchive.setDepartmentLevel3(value);
                break;
            default:
                // 其他字段可按需处理
                break;
        }
    }


    /**
     * 将单个HBS数据项转换为HbsBusinessArchive对象
     */
    private HbsBusinessArchive convertItemToBusinessArchive(JSONObject item, Map<String, JSONObject> fieldNameConfigMap, Map<String, String> fieldIdToNameMap) {
        HbsBusinessArchive businessArchive = new HbsBusinessArchive();

        // 设置主键
        String itemId = item.getStr("item_id");
        businessArchive.setId(itemId);
        // 处理字段数据
        JSONObject fields = item.getJSONObject("fields");
        if (fields != null) {
            for (Map.Entry<String, Object> entry : fields.entrySet()) {
                String fieldId = entry.getKey();
                Object fieldValue = entry.getValue();

                // 通过字段ID找到字段名称
                String fieldName = fieldIdToNameMap.get(fieldId);
                if (fieldName != null) {
                    JSONObject fieldConfig = fieldNameConfigMap.get(fieldName);
                    if (fieldConfig != null) {
                        // 使用转换器处理字段值
                        String convertedValue = hbsDataConverter.convertFieldValue(fieldConfig, fieldValue);
                        // 根据字段名称映射到HbsBusinessArchive属性
                        setBusinessArchiveFieldByName(businessArchive, fieldName, convertedValue);
                    }
                }
            }
        }
        // 设置创建和更新时间
        String createdOnStr = item.getStr("created_on");
        if (createdOnStr != null && !createdOnStr.isEmpty()) {
            businessArchive.setCreatedOn(parseDateString(createdOnStr));
        }

        String updatedOnStr = item.getStr("updated_on");
        if (updatedOnStr != null && !updatedOnStr.isEmpty()) {
            businessArchive.setUpdatedOn(parseDateString(updatedOnStr));
        }
        return businessArchive;
    }

    /**
     * 根据字段名称设置HbsBusinessArchive对象的对应属性
     */
    private void setBusinessArchiveFieldByName(HbsBusinessArchive businessArchive, String fieldName, String value) {
        switch (fieldName) {
            case "档案状态":
                businessArchive.setArchiveStatus(value);
                break;
            case "合作类型":
                businessArchive.setCooperationType(value);
                break;
            case "客户编号":
                businessArchive.setCustomerCode(value);
                break;
            case "客户名称":
                businessArchive.setCustomerName(value);
                break;
            case "客户类型":
                businessArchive.setCustomerType(value);
                break;
            case "证照情况":
                businessArchive.setLicenseStatus(value);
                break;
            case "一级部门":
                businessArchive.setDepartmentLevel1(value);
                break;
            case "二级部门":
                businessArchive.setDepartmentLevel2(value);
                break;
            case "业务区域":
                businessArchive.setBusinessArea(value);
                break;
            case "负责销售(作废)":
                businessArchive.setSalesPerson(value);
                break;
            case "省份":
                businessArchive.setProvince(value);
                break;
            case "城市":
                businessArchive.setCity(value);
                break;
            case "区县":
                businessArchive.setDistrict(value);
                break;
            case "单位地址":
                businessArchive.setAddress(value);
                break;
            case "联系人":
                businessArchive.setContactPerson(value);
                break;
            case "开户行&账号":
                businessArchive.setBankAccount(value);
                break;
            case "税号":
                businessArchive.setTaxId(value);
                break;
            case "电话":
                businessArchive.setPhone(value);
                break;
            case "传真":
                businessArchive.setFax(value);
                break;
            case "邮编":
                businessArchive.setZipcode(value);
                break;
            case "网址":
                businessArchive.setWebsite(value);
                break;
            case "备注":
                businessArchive.setRemark(value);
                break;
            case "应收余额":
                businessArchive.setReceivableBalance(value);
                break;
            case "在用品规":
                businessArchive.setInUseSpecs(value);
                break;
            case "采购记录":
                businessArchive.setPurchaseRecords(value);
                break;
            case "首营触发":
                businessArchive.setFirstSaleTrigger(value);
                break;
            case "流向提交方式":
                businessArchive.setFlowSubmissionMethod(value);
                break;
            case "流向上传记录":
                businessArchive.setFlowUploadRecords(value);
                break;
            case "发货>签订地址":
                businessArchive.setShippingAddress(value);
                break;
            case "发货>供方授权代表":
                businessArchive.setShippingRepresentative(value);
                break;
            case "发货>联系电话":
                businessArchive.setShippingContact(value);
                break;
            case "发货>公司电话":
                businessArchive.setShippingCompanyPhone(value);
                break;
            case "发货>单位地址":
                businessArchive.setShippingUnitAddress(value);
                break;
            case "发货>仓库地址":
                businessArchive.setShippingWarehouseAddress(value);
                break;
            case "发货>开户行&账号":
                businessArchive.setShippingBankAccount(value);
                break;
            case "发货>操作人员":
                businessArchive.setShippingOperator(value);
                break;
            case "业务关联人":
                businessArchive.setBusinessAssociate(value);
                break;
            case "所有人可用":
                businessArchive.setAvailableToAll(value);
                break;
            case "备查状态":
                businessArchive.setBackupStatus(value);
                break;
            case "商业数据":
                businessArchive.setCommercialData(value);
                break;
            case "档案来源":
                businessArchive.setArchiveSource(value);
                break;
            default:
                // 其他字段可按需处理
                break;
        }
    }


    /**
     * 将单个HBS数据项转换为HbsTerminalArchive对象
     */
    private HbsTerminalArchive convertItemToTerminalArchive(JSONObject item, Map<String, JSONObject> fieldNameConfigMap, Map<String, String> fieldIdToNameMap) {
        HbsTerminalArchive terminalArchive = new HbsTerminalArchive();

        // 设置主键
        String itemId = item.getStr("item_id");
        terminalArchive.setId(itemId);

        // 处理字段数据
        JSONObject fields = item.getJSONObject("fields");
        if (fields != null) {
            for (Map.Entry<String, Object> entry : fields.entrySet()) {
                String fieldId = entry.getKey();
                Object fieldValue = entry.getValue();

                // 通过字段ID找到字段名称
                String fieldName = fieldIdToNameMap.get(fieldId);
                if (fieldName != null) {
                    JSONObject fieldConfig = fieldNameConfigMap.get(fieldName);
                    if (fieldConfig != null) {
                        // 使用转换器处理字段值
                        String convertedValue = hbsDataConverter.convertFieldValue(fieldConfig, fieldValue);
                        // 根据字段名称映射到HbsTerminalArchive属性
                        setTerminalArchiveFieldByName(terminalArchive, fieldName, convertedValue);
                    }
                }
            }
        }

        // 设置创建和更新时间
        String createdOnStr = item.getStr("created_on");
        if (createdOnStr != null && !createdOnStr.isEmpty()) {
            terminalArchive.setCreatedOn(parseDateString(createdOnStr));
        }

        String updatedOnStr = item.getStr("updated_on");
        if (updatedOnStr != null && !updatedOnStr.isEmpty()) {
            terminalArchive.setUpdatedOn(parseDateString(updatedOnStr));
        }

        return terminalArchive;
    }

    /**
     * 根据字段名称设置HbsTerminalArchive对象的对应属性
     */
    private void setTerminalArchiveFieldByName(HbsTerminalArchive terminalArchive, String fieldName, String value) {
        switch (fieldName) {
            case "档案状态":
                terminalArchive.setArchiveStatus(value);
                break;
            case "开发状态":
                terminalArchive.setDevelopmentStatus(value);
                break;
            case "终端ID":
                terminalArchive.setTerminal(value);
                break;
            case "终端名称":
                terminalArchive.setTerminalName(value);
                break;
            case "别名/曾用名":
                terminalArchive.setAliasName(value);
                break;
            case "终端大类":
                terminalArchive.setTerminalCategory(value);
                break;
            case "终端小类":
                terminalArchive.setTerminalSubcategory(value);
                break;
            case "终端性质":
                terminalArchive.setTerminalNature(value);
                break;
            case "几级医院":
                terminalArchive.setHospitalLevel(value);
                break;
            case "几等医院":
                terminalArchive.setHospitalGrade(value);
                break;
            case "定点医保":
                terminalArchive.setMedicalInsurance(value);
                break;
            case "门诊量":
                if (value != null && !value.isEmpty()) {
                    try {
                        terminalArchive.setOutpatientVolume(Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        log.warn("门诊量转换失败: {}", value);
                    }
                }
                break;
            case "床位数":
                if (value != null && !value.isEmpty()) {
                    try {
                        terminalArchive.setBedCount(Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        log.warn("床位数转换失败: {}", value);
                    }
                }
                break;
            case "单位简介":
                terminalArchive.setUnitDescription(value);
                break;
            case "所在省份":
                terminalArchive.setProvince(value);
                break;
            case "所在城市":
                terminalArchive.setCity(value);
                break;
            case "所在区县":
                terminalArchive.setDistrict(value);
                break;
            case "详细地址":
                terminalArchive.setDetailedAddress(value);
                break;
            case "代理商数":
                if (value != null && !value.isEmpty()) {
                    try {
                        terminalArchive.setAgentCount(Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        log.warn("代理商数转换失败: {}", value);
                    }
                }
                break;
            case "漏斗品规数":
                if (value != null && !value.isEmpty()) {
                    try {
                        terminalArchive.setFunnelSpecsCount(Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        log.warn("漏斗品规数转换失败: {}", value);
                    }
                }
                break;
            case "入院品规数":
                if (value != null && !value.isEmpty()) {
                    try {
                        terminalArchive.setAdmittedSpecsCount(Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        log.warn("入院品规数转换失败: {}", value);
                    }
                }
                break;
            case "总计采购数":
                if (value != null && !value.isEmpty()) {
                    try {
                        terminalArchive.setTotalPurchaseCount(Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        log.warn("总计采购数转换失败: {}", value);
                    }
                }
                break;
            case "自增编号":
                terminalArchive.setAutoNumber(value);
                break;
            case "批量":
                terminalArchive.setBatchInfo(value);
                break;
            case "文本":
                terminalArchive.setTextField(value);
                break;
            case "开发统计":
                // 将中文值转换为对应的整数
                if (value != null && !value.isEmpty()) {
                    if ("纳入".equals(value)) {
                        terminalArchive.setDevelopmentStatistic("1");
                    } else if ("不纳入".equals(value)) {
                        terminalArchive.setDevelopmentStatistic("0");
                    } else {
                        terminalArchive.setDevelopmentStatistic(value);
                    }
                }
                break;
            case "所缺终端":
                terminalArchive.setMissingTerminal(value);
                break;
            case "数据来源":
                terminalArchive.setDataSource(value);
                break;
            case "机构类型":
                terminalArchive.setInstitutionType(value);
                break;
            case "医保机构码":
                terminalArchive.setMedicalInsuranceCode(value);
                break;
            case "社会信用码":
                terminalArchive.setSocialCreditCode(value);
                break;
            default:
                // 其他字段可按需处理
                break;
        }
    }


    /**
     * 处理数据并转换为 HbsTerminalArchive 对象
     */
    private List<HbsTerminalArchive> processTerminalArchiveData(R tableConfigResponse, String tableId, JSONObject filter) throws Exception {
        List<HbsTerminalArchive> result = new ArrayList<>();

        // 解析表格配置
        JSONObject tableConfig = (JSONObject) tableConfigResponse.getData();
        if (tableConfig == null) {
            return result;
        }

        JSONObject tableInfo = tableConfig.getJSONObject("table");
        if (tableInfo == null) {
            return result;
        }

        JSONArray fields = tableInfo.getJSONArray("fields");

        // 构建字段映射
        Map<String, JSONObject> fieldNameConfigMap = new HashMap<>();
        Map<String, String> fieldIdToNameMap = new HashMap<>();

        if (fields != null) {
            for (int i = 0; i < fields.size(); i++) {
                JSONObject field = fields.getJSONObject(i);
                String fieldName = field.getStr("name");
                fieldNameConfigMap.put(fieldName, field);
                String fieldId = field.getStr("field_id");
                fieldIdToNameMap.put(fieldId, fieldName);
            }
        }

        // 分页获取所有数据项
        List<Object> allItems = getItemsByPage(tableId, 100, filter); // 每页100条

        // 处理每一条数据
        for (Object itemObj : allItems) {
            if (itemObj instanceof JSONObject) {
                JSONObject item = (JSONObject) itemObj;
                HbsTerminalArchive terminalArchive = convertItemToTerminalArchive(item, fieldNameConfigMap, fieldIdToNameMap);
                if (terminalArchive != null) {
                    result.add(terminalArchive);
                }
            }
        }

        return result;
    }

    /**
     * 批量插入终端档案数据
     */
    private void batchInsertTerminalArchives(List<HbsTerminalArchive> terminalArchives) {
        if (CollectionUtils.isEmpty(terminalArchives)) {
            return;
        }

        // 过滤掉ID为空的记录
        List<HbsTerminalArchive> validArchives = terminalArchives.stream()
                .filter(archive -> archive.getId() != null && !archive.getId().isEmpty())
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(validArchives)) {
            return;
        }

        // 提取所有item_id
        List<String> itemIds = validArchives.stream()
                .map(HbsTerminalArchive::getId)
                .collect(Collectors.toList());

        // 查询已存在的记录
        List<HbsTerminalArchive> existingArchives = hbsTerminalArchiveService.listByIds(itemIds);
        Set<String> existingIds = existingArchives.stream()
                .map(HbsTerminalArchive::getId)
                .collect(Collectors.toSet());

        // 分离新增和更新的数据
        List<HbsTerminalArchive> toInsert = new ArrayList<>();
        List<HbsTerminalArchive> toUpdate = new ArrayList<>();

        for (HbsTerminalArchive archive : validArchives) {
            if (existingIds.contains(archive.getId())) {
                toUpdate.add(archive);
            } else {
                toInsert.add(archive);
            }
        }

        // 批量插入新数据
        if (!toInsert.isEmpty()) {
            int batchSize = 100;
            for (int i = 0; i < toInsert.size(); i += batchSize) {
                int endIndex = Math.min(i + batchSize, toInsert.size());
                List<HbsTerminalArchive> batch = toInsert.subList(i, endIndex);
                hbsTerminalArchiveService.saveBatch(batch);
            }
        }

        // 批量更新已有数据
        if (!toUpdate.isEmpty()) {
            int batchSize = 100;
            for (int i = 0; i < toUpdate.size(); i += batchSize) {
                int endIndex = Math.min(i + batchSize, toUpdate.size());
                List<HbsTerminalArchive> batch = toUpdate.subList(i, endIndex);
                hbsTerminalArchiveService.updateBatchById(batch);
            }
        }
    }

    /**
     * 分页获取所有数据项，支持过滤条件
     */
    private List<Object> getItemsByPage(String tableId, int pageSize, JSONObject filter) throws Exception {
        List<Object> allItems = new ArrayList<>();
        int offset = 0;

        // 设置合理的页面大小（不超过接口最大限制）
        int actualPageSize = Math.min(pageSize, 100);

        while (true) {
            // 构建请求参数
            JSONObject itemListRequest = new JSONObject();
            itemListRequest.set("table_id", tableId);
            itemListRequest.set("limit", actualPageSize);
            itemListRequest.set("offset", offset);

            // 添加排序参数
            JSONObject order = new JSONObject();
            order.set("field_id", "created_on");
            order.set("type", "desc");
            itemListRequest.set("order", order);

            // 添加过滤条件
            if (filter != null && !filter.isEmpty()) {
                itemListRequest.set("filter", filter);
            }

            // 获取当前页数据
            R itemListResponse = hbsBusinessController.openapiV1ItemList(itemListRequest);
            if (itemListResponse == null || R.isError(itemListResponse)) {
                break;
            }

            JSONObject data = (JSONObject) itemListResponse.getData();
            if (data == null) {
                break;
            }

            JSONArray items = data.getJSONArray("items");
            if (items == null || items.isEmpty()) {
                break;
            }
            // 添加当前页数据到结果列表
            allItems.addAll(items);
            // 检查是否还有更多数据
            Integer total = data.getInt("total");
            Integer currentCount = items.size();

            // 终止条件：当已获取的数据量达到总数时停止
            if (total != null && offset + currentCount >= total) {
                break;
            }

            // 更新偏移量
            offset += actualPageSize;
        }

        return allItems;
    }

    /**
     * 解析日期字符串
     */
    private Date parseDateString(String dateStr) {
        if (dateStr == null || dateStr.isEmpty()) {
            return null;
        }

        try {
            // 根据实际的日期格式进行解析，假设是 "yyyy-MM-dd HH:mm:ss" 格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            // 解析失败时返回null或当前时间
            return new Date();
        }
    }

    /**
     * 从表格列表中提取表ID
     */
    private String extractTableId(String tableConfigName, R response) {
        if (response == null || !R.isSuccess(response)) {
            return "";
        }

        JSONObject data = (JSONObject) response.getData();
        if (data == null) {
            return "";
        }
        JSONArray tables = data.getJSONArray("tables");
        if (tables != null) {
            for (int i = 0; i < tables.size(); i++) {
                JSONObject table = tables.getJSONObject(i);
                String tableName = table.getStr("name");
                String tableId = table.getStr("table_id");

                // 根据表名匹配表
                if (tableConfigName.equals(tableName)) {
                    return tableId;
                }
            }
        }
        return "";
    }


    /**
     * 并行分页获取所有数据项，支持过滤条件
     * 使用 while(hasMore) 循环，逐页拉取数据
     */
    private List<Object> getItemsByPageParallel(String tableId, int pageSize, JSONObject filter) throws Exception {
        List<Object> allItems = new ArrayList<>();
        ExecutorService executor = Executors.newFixedThreadPool(30);
        List<Future<JSONArray>> futures = new ArrayList<>();

        int offset = 0;
        boolean hasMore = true;

        // 使用 while(hasMore) 循环获取所有页面
        while (hasMore) { // 设置最大页数限制
            final int currentOffset = offset;
            Future<JSONArray> future = executor.submit(() -> {
                try {
                    JSONObject itemListRequest = new JSONObject();
                    itemListRequest.set("table_id", tableId);
                    itemListRequest.set("limit", pageSize);
                    itemListRequest.set("offset", currentOffset);

                    JSONObject order = new JSONObject();
                    order.set("field_id", "created_on");
                    order.set("type", "desc");
                    itemListRequest.set("order", order);

                    if (filter != null && !filter.isEmpty()) {
                        itemListRequest.set("filter", filter);
                    }

                    R itemListResponse = hbsBusinessController.openapiV1ItemList(itemListRequest);
                    if (itemListResponse == null || !R.isSuccess(itemListResponse)) {
                        return null;
                    }

                    JSONObject data = (JSONObject) itemListResponse.getData();
                    if (data == null) {
                        return null;
                    }

                    return data.getJSONArray("items");
                } catch (Exception e) {
                    log.error("获取偏移量{}的数据失败", currentOffset, e);
                    return null;
                }
            });

            futures.add(future);
            offset += pageSize;

            // 检查是否还有更多数据
            try {
                JSONObject checkRequest = new JSONObject();
                checkRequest.set("table_id", tableId);
                checkRequest.set("limit", 1);
                checkRequest.set("offset", offset);

                if (filter != null && !filter.isEmpty()) {
                    checkRequest.set("filter", filter);
                }

                R checkResponse = hbsBusinessController.openapiV1ItemList(checkRequest);
                if (checkResponse != null && R.isSuccess(checkResponse)) {
                    JSONObject data = (JSONObject) checkResponse.getData();
                    if (data != null) {
                        hasMore = Boolean.TRUE.equals(data.get("has_more"));
                    } else {
                        hasMore = false;
                    }
                } else {
                    hasMore = false;
                }
            } catch (Exception e) {
                log.warn("检查是否有更多数据时出错，停止获取更多数据", e);
                hasMore = false;
            }
        }

        // 收集所有结果
        for (Future<JSONArray> future : futures) {
            try {
                JSONArray items = future.get();
                if (items != null && !items.isEmpty()) {
                    for (int i = 0; i < items.size(); i++) {
                        allItems.add(items.get(i));
                    }
                }
            } catch (Exception e) {
                log.error("收集分页数据失败", e);
            }
        }

        executor.shutdown();
        return allItems;
    }


    /**
     * 获取表格列表、表格配置和数据列表
     */
    private tableResult getTableResult(JSONObject bodyJson) throws Exception {
        R tableListResponse = hbsBusinessController.openapiV1TableList(bodyJson);
        String tableConfigName = bodyJson.get("tableConfigName").toString();
        String tableId = extractTableId(tableConfigName, tableListResponse);
        if (!StringUtils.hasText(tableId)) {
            throw new RuntimeException("未找到" + tableConfigName + "表");
        }

        // 获取表格配置
        JSONObject tableConfigRequest = new JSONObject();
        R tableConfigResponse = hbsBusinessController.openapiV1TableConfig(tableId, tableConfigRequest);

        if (tableConfigResponse == null || !R.isSuccess(tableConfigResponse)) {
            throw new RuntimeException("获取表格配置失败");
        }

        // 不再调用 itemListRequest，因为要分页获取
        return new tableResult(tableConfigResponse, null, tableId);
    }

    /**
     * 从表格列表中提取表ID
     */
    private static class tableResult {
        public final R tableConfigResponse;
        public final R itemListResponse;
        public final String tableId;

        public tableResult(R tableConfigResponse, R itemListResponse, String tableId) {
            this.tableConfigResponse = tableConfigResponse;
            this.itemListResponse = itemListResponse;
            this.tableId = tableId;
        }
    }


}
